package com.topboya.linker.ui.activity;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.topboya.linker.AppConst;
import com.topboya.linker.R;
import com.topboya.linker.model.response.ProductInforListReponse;
import com.topboya.linker.model.response.SelectFileResponse;
import com.topboya.linker.ui.adapter.RecycleViewDivider;
import com.topboya.linker.ui.adapter.SeeSelectFileAdapter;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

//查看已经选择的文件资料
public class SeeSelectedFileActivity extends BaseActivity {

    private static final String TAG = "SeeSelectedFileActivity:";
    private TextView toolbar_title;
    private TextView tv_toolbar_right;
    private Toolbar toolbar;
    private RecyclerView rey_selectfile;
    private List<SelectFileResponse.FileList> selectFileList;
    private SeeSelectFileAdapter selectFileAdapter;
    private ArrayList<ProductInforListReponse.ProductDetailInfoList> productDetailInfoLists;


    @Override
    protected int getLayoutId() {

        return R.layout.activity_see_selectfile;
    }

    @Override
    protected void initView() {

        toolbar_title = findViewById(R.id.toolbar_title);
        rey_selectfile = findViewById(R.id.rey_selectfile);
        tv_toolbar_right = findViewById(R.id.tv_toolbar_right);
        toolbar = findViewById(R.id.toolbar);
    }

    @Override
    protected void initData() {

        tv_toolbar_right.setText("确定");
        selectFileList = (List<SelectFileResponse.FileList>) getIntent().getSerializableExtra("selectFileList");
        productDetailInfoLists = (ArrayList<ProductInforListReponse.ProductDetailInfoList>) getIntent().getSerializableExtra("productDetailInfoLists");
        toolbar_title.setText("已选择数量："+selectFileList.size());

        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
        rey_selectfile.setLayoutManager(layoutManager);
//        rey_selectfile.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, 2, 0xBFC0C2));
        selectFileAdapter = new SeeSelectFileAdapter(this, (ArrayList<SelectFileResponse.FileList>) selectFileList);
        rey_selectfile.setAdapter(selectFileAdapter);


    }

    @Override
    protected void initListener() {

        toolbar.setNavigationOnClickListener(v -> {    back();});
        selectFileAdapter.setOnItemClickListener((v, position) -> {
//               点击删除按钮进行删除
            AppConst.FILE_SELECTED_MAP.remove(selectFileList.get(position).getId());
            selectFileList.remove(position);
            selectFileAdapter.notifyDataSetChanged();

        });
        tv_toolbar_right.setOnClickListener(v -> {
            back();
            });
    }

    @Override
    protected BasePresenter createPresenter() {

        return null;
    }

    //   重写返回键，怕用户点删除按钮了，直接返回
    @Override
    public void onBackPressed() {

        back();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 是否触发按键为back键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        } else { // 如果不是back键正常响应
            return super.onKeyDown(keyCode, event);
        }
    }

    private void back() {

         Intent intent = new Intent();
         intent.putExtra("fileLists", (Serializable) selectFileList);
         setResult(1002,intent);
         finish();

    }
}
